package com.campusflea;

import android.app.Activity;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.support.annotation.RequiresApi;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;

import com.campusflea.DateBase.ShopInfo;
import com.campusflea.custom.ClearWriteEditText;
import com.campusflea.utils.PathGetter;

import java.io.File;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UploadFileListener;


public class ReleaseActivity extends Activity{
    private Button btn_release;
    private ImageView imageView;
    private ClearWriteEditText edit_name,edit_disc,edit_money;
    private Spinner spinner;
    private ArrayAdapter<CharSequence> adapterShop = null;
    private static String[] shopItem={"校园代步","手机电脑","电子配件","运动健身","图书教材","衣物伞帽","生活娱乐","其他"};
    private Uri uri;
    private String shopType;
    private String img_path;
    private String publisher;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setContentView(R.layout.release_main);
        //requestWindowFeature(Window.FEATURE_NO_TITLE);
        btn_release = (Button)findViewById(R.id.release_btn);
        imageView = (ImageView)findViewById(R.id.imageView);
        edit_name = (ClearWriteEditText)findViewById(R.id.clearWriteEditText);
        edit_disc = (ClearWriteEditText)findViewById(R.id.clearWriteEditText1);
        edit_money = (ClearWriteEditText)findViewById(R.id.clearWriteEditText2);
        spinner = (Spinner)findViewById(R.id.spinner);
        Bmob.initialize(this, "36b62198745ed1900e5f1e8d55eb660d");
        Intent intent = getIntent();
        publisher = intent.getExtras().getString("phone");
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                shopType = parent.getItemAtPosition(position).toString();
                Toast.makeText(ReleaseActivity.this, "选择的商品是：" + shopType,
                        Toast.LENGTH_LONG).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub
            }
        });
        //将数据cityInfo填充到适配器adapterCity中
        adapterShop = new ArrayAdapter<CharSequence>(this,
                android.R.layout.simple_spinner_dropdown_item, shopItem);
        //设置下拉框的数据适配器adapterCity
        spinner.setAdapter(adapterShop);

        //添加图片
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK, null);
                intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        "image/*");
                startActivityForResult(intent, 0x1);
            }
        });
        //发表
        btn_release.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.KITKAT)
            @Override
            public void onClick(View v) {

                File file = new File(PathGetter.getPath(ReleaseActivity.this, uri));
                final BmobFile bmobFile = new BmobFile(file);
                Toast.makeText(ReleaseActivity.this,PathGetter.getPath(ReleaseActivity.this, uri),Toast.LENGTH_LONG).show();
                //Toast.makeText(ReleaseActivity.this,bmobFile.toString(),Toast.LENGTH_LONG).show();

                bmobFile.uploadblock(new UploadFileListener() {

                    @Override
                    public void done(BmobException e) {
                        ShopInfo shopInfo = new ShopInfo();
                        shopInfo.setBmobFile(bmobFile);
                        shopInfo.setShop_name(edit_name.getText().toString());
                        shopInfo.setShop_disc(edit_disc.getText().toString());
                        shopInfo.setShop_price(edit_money.getText().toString());
                        shopInfo.setShop_type(shopType);
                        shopInfo.setPublisher(publisher);
                        shopInfo.save(new SaveListener() {
                            @Override
                            public void done(Object o, Object o2) {

                            }

                            @Override
                            public void done(Object o, BmobException e) {
                                Toast.makeText(ReleaseActivity.this,"发布成功",Toast.LENGTH_LONG).show();
                            }
                        });
                        Toast.makeText(ReleaseActivity.this, "上传成功", Toast.LENGTH_SHORT).show();
                    }

                });
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        if (requestCode == 0x1 && resultCode == RESULT_OK) {
            if (data != null) {
                uri=data.getData();
                imageView.setImageURI(uri);
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }


}
